package com.hqyj.fj.javaSpringBoot.modules.account.dao;

import com.hqyj.fj.javaSpringBoot.modules.account.entity.Resource;
import com.hqyj.fj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.fj.javaSpringBoot.modules.test.entity.City;
import com.hqyj.fj.javaSpringBoot.modules.test.entity.Student;
import com.mysql.fabric.xmlrpc.base.Data;
import org.apache.ibatis.annotations.*;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


@Mapper
@Repository
public interface RoleDao  {

    @Insert("insert into account_role (create_date,role_name) " +
            "values (#{createDate}, #{roleName})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insertRole(Role role);
//check the manual that corresponds to your MySQL server version for the right syntax to use near
    @Update("update account_role set role_name=#{roleName} where id=#{id}")
    int updateRole( Role role);

    @Delete("delete from account_role where id=#{id}")
    void  deleteRole(int id);

    @Select("select * from account_role where id=#{id}")
   Role getRoleById(int id);

    @Select("select * from account_role ar left join account_user_role aur on ar.id = aur.role_id "
            + "where aur.user_id = #{userId}")
    List<Role> getRolesByUserId(@Param("userId") int userId);

    @Select("select * from account_role ar left join account_role_resource arr on ar.id = arr.role_id "
            + "where arr.resource_id = #{resourceId}")
    List<Role> getRolesByResourceId(int resourceId);

    @Select("select * from account_role")
    List<Role> getRoles();

    @Select("<script>"
            + "select * from account_role "
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Role> getRolesBySearchBean(SearchBean searchBean);

    @Insert("<script>"
            + "insert into account_role  (create_date,role_name) values "
            + "<foreach collection='roles' item='role' index='index' separator=','>"
            + "(#{role.createDate}, #{role.roleName}) "
            + "</foreach>"
            + "</script>")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertRoles(@Param("roles") List<Role> roles);


}
